import Mock from 'mockjs'

// 保存注册用户的数据
const registerData = []

// 返回的默认登录信息
const loginInfo = Mock.mock({
    username: 'admin',
    password: 123456
})

// 返回的注册id
const registerId = Mock.mock({
    "registerId|+1": 1
});

// 首页数据
let homeDataList = Mock.mock({
    dataList: [  
        {
            cateName: '服饰类',
            cateId: 1,
            cateData: [
                {
                    name: '2022秋冬新款', price: 2680, express: '免运费', remain: 19, img: ['https://i.loli.net/2021/12/03/mpvXgUTdwP39Zuh.jpg'], id: '00001',
                },
                {
                    name: '羊毛衫男女', price: 2680, express: '免运费', remain: 19, img: ['https://i.loli.net/2021/12/03/tn47i3kDF1y8IYf.jpg'], id: '00002',
                },
                {
                    name: '秋冬大衣情侣', price: 2680, express: '免运费', remain: 19, img: ['https://i.loli.net/2021/12/03/j84SAraYWe1lh7d.jpg'], id: '00003',
                },
                {
                    name: '保暖三件套', price: 2680, express: '免运费', remain: 19, img: ['https://i.loli.net/2021/12/03/xL7kafWBM6oJ9RE.jpg'], id: '00004',
                },
                {
                    name: '光腿神器', price: 2680, express: '免运费', remain: 19, img: ['https://i.loli.net/2021/12/03/tQzuxGdA2yiKrsH.jpg'], id: '00005',
                },
                {
                    name: '牛仔裤系列', price: 2680, express: '免运费', remain: 19, img: ['https://i.loli.net/2021/12/03/ZNApMu6mLXykFnJ.jpg'], id: '00006',
                }
            ]
        },
        {
            cateName: '食品类',
            cateId: 2,
            cateData: [
                {
                    name: '2022新上架', price: 2680, express: '免运费', remain: 19, img: ['https://i.loli.net/2021/12/03/XKxtAMpWwLaV8iF.jpg'], id: '0001',
                },
                {
                    name: '绝味之各种辣', price: 2680, express: '免运费', remain: 19, img: ['https://i.loli.net/2021/12/03/tIEUQ21XqVdnzxF.jpg'], id: '0002',
                },
                {
                    name: '甜甜面包房', price: 2680, express: '免运费', remain: 19, img: ['https://i.loli.net/2021/12/03/d9EpW2uBZfG3nV1.jpg'], id: '0003',
                },
                {
                    name: '火锅、麻辣烫', price: 2680, express: '免运费', remain: 19, img: ['https://i.loli.net/2021/12/03/UZTvoditBy1pWFO.jpg'], id: '0004',
                },
                {
                    name: '嗦粉快乐房', price: 2680, express: '免运费', remain: 19, img: ['https://i.loli.net/2021/12/03/RcEmIPuOLn5d9Vg.jpg'], id: '0005',
                },
                {
                    name: '清淡菜系', price: 2680, express: '免运费', remain: 19, img: ['https://i.loli.net/2021/12/03/6CvmGD2zsRnch3k.jpg'], id: '0006',
                }
            ]
        },
        {
            cateName: '家居类',
            cateId: 3,
            cateData: [
                {
                    name: '2022全新家具', price: 2680, express: '免运费', remain: 19, img: ['https://i.loli.net/2021/12/03/yk3HG7zcVwTuW8S.jpg'], id: '001',
                },
                {
                    name: '沙发床、沙发摇篮', price: 2680, express: '免运费', remain: 19, img: ['https://i.loli.net/2021/12/03/xH6mp8QWqfVycn2.jpg'], id: '002',
                },
                {
                    name: '电视、洗衣机类', price: 2680, express: '免运费', remain: 19, img: ['https://i.loli.net/2021/12/03/Wx5J1iIfB8eAYjV.jpg'], id: '003',
                },
                {
                    name: '厨房用品类', price: 2680, express: '免运费', remain: 19, img: ['https://i.loli.net/2021/12/03/5yCm9cluNTA2174.jpg'], id: '004',
                },
                {
                    name: '卧室装饰品类', price: 2680, express: '免运费', remain: 19, img: ['https://i.loli.net/2021/12/03/of5gTCWs27G81mt.jpg'], id: '005',
                },
                {
                    name: '客厅大型用品类', price: 2680, express: '免运费', remain: 19, img: ['https://i.loli.net/2021/12/03/qcT13dUbFhlDmfX.jpg'], id: '006',
                }
            ]
        }
    ]
});


// 配置拦截匹配和处理回调
// 登录接口
Mock.mock('http://localhost:5173/login', 'post', config => {
    let data = JSON.parse(config.body)
    // console.log(data);
    console.log(registerData);
    let res = registerData.filter(item => {
        // console.log(item);
        return item.username === data.username && item.password === parseInt(data.password)
    });
    // console.log(res);
    if (res.length != 0) {
        return Mock.mock(res[0]);
    } else {
        return loginInfo;
    }
})

// 注册接口
Mock.mock('http://localhost:5173/register', 'post', config => {
    let data = JSON.parse(config.body)
    let registerUser = {
        username: data.username,
        password: parseInt(data.password)
    }

    registerData.push(registerUser)
    // console.log(registerData);
    return registerId;
})

// 获取首页数据接口
Mock.mock('http://localhost:5173/homedata', 'get', homeDataList)

// 详情页数据
Mock.mock(`http://localhost:5173/detail`, 'post', config => {
	let res = JSON.parse(config.body)
	let dataList = homeDataList.dataList
	for(var i in dataList){
		let items = dataList[i].cateData
		for(var j in items){
			if(items[j].id == res.id){
				items[j].price = 1
				items[j].desc = '示例商品'
				return items[j]
			}
		}
	}
})